﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ExhibitionDraw.Control
{
    public partial class ImageButton : UserControl
    {
        public event EventHandler Click = null;
        public ImageButton()
        {
            InitializeComponent();

            this.MouseLeftButtonDown += new MouseButtonEventHandler(ImageButton_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(ImageButton_MouseLeftButtonUp);
            this.MouseEnter += new MouseEventHandler(ImageButton_MouseEnter);
            this.MouseLeave += new MouseEventHandler(ImageButton_MouseLeave);
            this.SizeChanged += new SizeChangedEventHandler(ImageButton_SizeChanged);

            this.image1.Margin = new Thickness(0, 0, 0, 0);
            if (this.ImageSourceNormal != null)
            {
                this.image1.Source = this.ImageSourceNormal;
                //this.LayoutRoot.Background = this.ImageSourceNormal as DependencyObject;
            }
            ImageButton_SizeChanged(null, null);
        }

        private void ImageButton_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.image1.Width = this.Width;
            this.image1.Height = this.Height;
        }
        private void ImageButton_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this.ImageSourceNormal != null)
            {
                this.image1.Source = this.ImageSourceNormal;
            }
        }
        private void ImageButton_MouseEnter(object sender, MouseEventArgs e)
        {
            if (this.ImageSourceOverlap != null)
            {
                this.image1.Source = this.ImageSourceOverlap;
            }
        }
        private void ImageButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.ImageSourceDown != null)
            {
                this.image1.Source = this.ImageSourceDown;
            }
        }
        private void ImageButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.ImageSourceOverlap != null)
            {
                this.image1.Source = this.ImageSourceOverlap;
            }
            //单击事件
            if (this.Click != null)
            {
                this.Click(sender, e);
            }
        }


        //定义正常显示图片源
        public ImageSource ImageSourceNormal
        {
            get { return base.GetValue(ImageSourceNormalProperty) as ImageSource; }
            set
            {
                base.SetValue(ImageSourceNormalProperty, value);
                this.image1.Source = value;
            }
        }
        public static readonly DependencyProperty ImageSourceNormalProperty = DependencyProperty.Register("ImageSourceNormal", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));

        //定义Overlap显示图片源
        public ImageSource ImageSourceOverlap
        {
            get { return base.GetValue(ImageSourceOverlapProperty) as ImageSource; }
            set { base.SetValue(ImageSourceOverlapProperty, value); }
        }
        public static readonly DependencyProperty ImageSourceOverlapProperty = DependencyProperty.Register("ImageSourceOverlap", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));

        //定义Down显示图片源        
        public ImageSource ImageSourceDown
        {
            get { return base.GetValue(ImageSourceDownProperty) as ImageSource; }
            set { base.SetValue(ImageSourceDownProperty, value); }
        }
        public static readonly DependencyProperty ImageSourceDownProperty = DependencyProperty.Register("ImageSourceDown", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));

    }
}

